package com.lenovo.smartengine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.dolby.api.DsAccess;
import com.dolby.api.DsConstants;
import com.dolby.api.DsGlobalEx;
import com.dolby.api.IDsAccessEvents;
import com.dolby.api.IDsEvents;
import com.lenovo.smartengine.service.SmartEngineService;


public class MainActivity extends Activity implements OnClickListener,IDsAccessEvents,IDsEvents{
	private final static String TAG = "MainActivity";
	private Button sendAudioBtn = null;
	private Button sendVideoBtn = null;
	private Button sendSysAudioBtn = null;
	private Button startServiceBtn = null;
    private  DsGlobalEx mDsClient = null;
    
	int phonemediatype = -1;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendAudioBtn = (Button)findViewById(R.id.button1);
        sendVideoBtn = (Button)findViewById(R.id.button2);
        sendSysAudioBtn = (Button)findViewById(R.id.button3);
        startServiceBtn = (Button)findViewById(R.id.startService);
        
        sendAudioBtn.setOnClickListener(this);
        sendVideoBtn.setOnClickListener(this);
        sendSysAudioBtn.setOnClickListener(this);
        startServiceBtn.setOnClickListener(this);
        
		Log.i("MainActivity","startService .....");
		Intent intent4 = new Intent();
		intent4.setClass(MainActivity.this,SmartEngineService.class);
		startService(intent4);
    }

    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    }
    
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		int id = view.getId();
		switch(id){
		case R.id.button1:
			Intent intent1 = new Intent();
			intent1.putExtra("media_type", "audio");
			intent1.putExtra("type_value", 1001);
			intent1.setAction("com.lenovo.action.audiotype");
			this.sendBroadcast(intent1);
			break;
		case R.id.button2:
			Intent intent2 = new Intent();
			intent2.putExtra("media_type", "video");
			intent2.putExtra("type_value", 1000);
			intent2.setAction("com.lenovo.action.audiotype");
			this.sendBroadcast(intent2);
			break;
		case R.id.button3:
			Intent intent3 = new Intent();
			intent3.putExtra("media_type", "sysAudio");
			intent3.putExtra("type_value", 1002);
			intent3.setAction("com.lenovo.action.audiotype");
			this.sendBroadcast(intent3);
			break;
		case R.id.startService:
			Log.i("MainActivity","press startServiceBtn");
			Intent intent4 = new Intent();
			intent4.putExtra("media_type", "audio");
			intent4.putExtra("type_value", 1001);
			intent4.setClass(MainActivity.this,SmartEngineService.class);
			startService(intent4);
			break;
			
		}
	}
	
	@Override
	public void onAccessAvailable() {
		// TODO Auto-generated method stub
	      Log.d(TAG, "onAccessAvailable() ------------------");
	        if ((null != mDsClient) && (DsAccess.THIS_APP_GRANTED != mDsClient.checkAccessRight())) {
	            if (DsConstants.DS_NO_ERROR != mDsClient.requestAccessRight()) {
	                Log.d(TAG, "Fail to get access right");
	                return;
	            }
	        }
	        Log.d(TAG, "Got access right");
	}

	@Override
	public void onAccessForceReleased(String arg0, int arg1) {
		// TODO Auto-generated method stub
        Log.d(TAG, "onAccessForceReleased() ------------------");
	}

	@Override
	public boolean onAccessRequested(String arg0, int arg1) {
		// TODO Auto-generated method stub
	       Log.d(TAG, "onAccessRequested() ------------------");
		return false;
	}

	@Override
	public void onClientConnected() {
		// TODO Auto-generated method stub
		 Log.d(TAG, "onClientConnected() ------------------");

	        // register Ds Event listener
	        mDsClient.registerDsEvents(this);
	        Log.d(TAG, "Register DsEvent listener");

	        int result = mDsClient.requestAccessRight();
	        if (result != DsConstants.DS_NO_ERROR)
	        {
	            Log.d(TAG, "onClientConnected() request audio focuse failed, result = " + result);

	            // DS2API-TODO: ......??? When and Where should application request audio focus again???

	            return;
	        } else {
	            Log.d(TAG, "onClientConnected() request audio focuse successfully!");
	        }
	        // Client has connected.
	}

	@Override
	public void onClientDisconnected() {
		// TODO Auto-generated method stub
        Log.d(TAG, "onClientDisconnected()");
	}
	
	@Override
	public void onDsOn(boolean on) {
		// TODO Auto-generated method stub
		Log.d(TAG,"onDsOn(" + on + ")");
	}

	@Override
	public void onDsSuspended(boolean isSuspended) {
		// TODO Auto-generated method stub
        Log.d(TAG,"onDsSuspended(" + isSuspended + ")");
	}

	@Override
	public void onProfileSelected(int profile) {
		// TODO Auto-generated method stub
		Log.d(TAG,"onProfileSelected(" + profile + ")");
	}

	@Override
	public void onProfileSettingsChanged(int profile) {
		// TODO Auto-generated method stub
	      Log.d(TAG,"onProfileSettingsChanged(" + profile + ")");
	}
}
